home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / ASMVLA00.ZIP / ASM1.TXT < prev    next >
Text File  |  1993-03-21  |  3KB  |  101 lines

  1. ────────────────────────────────────────────────────────────────────────────
  2.     ASM1.ASM - print a string
  3. ────────────────────────────────────────────────────────────────────────────
  4.  
  5.     Well, here's the classic example for the first program in just about
  6. every language.  It prints a message to the screen by using a DOS function.
  7. More specifically, it uses function 9 of interrupt 21h.  Here's the mock
  8. specification for the function:
  9.  
  10. ■-
  11. |IN:     ah = 9      ;ah tells INT 21h which function you want
  12. |        DS:DX = FAR pointer to the string to be printed.
  13. |                    ;the string must terminate with a dollar sign ($)
  14. |
  15. |OUT:    Prints the string to the screen
  16. ■-
  17.  
  18.     Other than that function, there's nothing new that can't easily be
  19. figured out.  The directive SEG, as you might have guessed, returns the 
  20. segment that the specified label is in.  OFFSET returns the offset from 
  21. the begining of the segment to the specified label.  Together, you can
  22. form a FAR pointer to a specified label.
  23.  
  24.     Another thing you might wonder about is why I put the SEG Message into
  25. AX and THEN Put it in DS.  
  26.  
  27.     The answer is:  You have to.  An immediate value cannot be put into a 
  28. segment register, but a register or an indexed value can.  For instance:
  29.  
  30. These are legal:
  31.  
  32. :   mov     DS,AX
  33. :   mov     DS,[TheSegment]
  34.  
  35. But these are not:
  36.  
  37. :   mov     DS,50
  38. :   mov     DS,0a000h
  39.     
  40.     One last piece of info: in the lines:
  41.  
  42. :Message     db  "This was printed using function 9 " 
  43. :            db  "of the DOS interrupt 21h.$"
  44.  
  45.     The DB is just a data type.  Its the same as a CHAR in C, which is 1 byte
  46.     in length.
  47.  
  48.     Other common data types are:
  49.  
  50.     DW  same as an INT in C - 2 bytes
  51.     DD  same as a double int or long int or a FAR pointer - 4 bytes
  52.  
  53.  
  54.     Well, that's pretty much it for this short section...  Try playing around
  55. with the 'print' function... Ya learn best by playing with it.
  56.  
  57.  
  58. One last side note:
  59.     I COULD have put the message in the CODE segment instead, by doing this:
  60.     
  61. ────────────────────
  62.  
  63.     DOSSEG
  64.     .MODEL SMALL
  65.     .STACK 200h
  66.     .CODE
  67.  
  68. Message     db  "Hey look! I'm in the code segment!$" 
  69.             
  70. START:
  71.     mov     ax,cs   ;since CS already points to the same segment as Message,
  72.     mov     ds,ax   ;I don't have to explicitly load the segment that message
  73.                     ;is in..
  74.  
  75.     mov     dx,offset Message
  76.     mov     ah,9
  77.     int     21h
  78.  
  79.     mov     ax,4c00h    ;Returns control to DOS
  80.     int     21h         ;MUST be here! Program will crash without it!
  81.  
  82. END START
  83.  
  84. ────────────────────
  85.  
  86.     The advantage to having all your data in the CODE segment is that DS and
  87. ES can be pointing anywhere and you can still access your data via a segment
  88. override!  
  89.  
  90.     Example:
  91.         say I'm in the middle of copying one section of the screen memory to
  92.     another and I need to access the variable "NumLines" I'd do it like this:
  93.  
  94. ────────
  95.  
  96.     mov ax,[CS:NumLines]    ;this is in IDEAL mode
  97.             ^^^
  98. ────────    Code Segment override
  99.  
  100.     Pretty flexable, eh?
  101.